#ifndef USER_PLUGIN_Terminator
#define USER_PLUGIN_Terminator
#include <iostream>
#include <string>
#include <map>
#include "../../libpland/lib/plugin.hpp"
#include <unistd.h>
#include <stdlib.h>

using namespace std;

using namespace pland;

class Terminator : public UserC_Plugin
{
public:
    Terminator();
    ~Terminator();
    void start();
    void stop();
    void load_queues(SharedDQueue front ,SharedDQueue back);
    void setId(int id);
private:
    int id_;
    SharedDQueue front_;
    SharedDQueue back_;
    bool running_;
	std::ofstream logFile_;
};

#endif


Terminator::Terminator()
{
	
    running_ = false;

}

void Terminator::start()
{
	logFile_.open((std::string("terminatorLog") + utill_to_string(id_) + ".txt").c_str(),ios::out);
    running_ = true;
    while(running_)
    {
        SharedData next = front_->pop();
        struct timeval tv;
        gettimeofday(&tv,NULL);
		logFile_ << (tv.tv_sec - next->get_header()->secLocal)*1000000 + tv.tv_usec - next->get_header()->usecLocal << endl;
    }
}

void Terminator::setId(int id)
{
    id_ = id;
}

void Terminator::stop()
{
    running_ = false;

}

void Terminator::load_queues(SharedDQueue front ,SharedDQueue back)
{
    front_  = front;
    back_  = back;
}


extern "C"
{

UserC_Plugin* Core_GetInstance()
{
    return new Terminator();
}

void Core_KillInstance(UserC_Plugin* a)
{
    delete a;
}

}
